package com.vincent.kdp.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.TypedValue;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.vincent.baseapp.util.SPUtil;
import com.vincent.kdp.R;
import com.vincent.kdp.adapter.NoticeListAdapter;
import com.vincent.kdp.bean.Paginator;
import com.vincent.kdp.http.RequestCallBack;
import com.vincent.kdp.ui.bean.response.NoticeAgentMatterListOutput;
import com.vincent.kdp.ui.decoration.DividerItemDecoration;
import com.vincent.kdp.ui.notice.repository.NoticeRepository;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class NoticeListActivity extends AppCompatActivity {

    @Bind(R.id.back_img)
    ImageView mBackImg;
    @Bind(R.id.toolbar_center_tv)
    TextView mToolbarCenterTv;

    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    @Bind(R.id.SwipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;

    private NoticeListActivity thisActivity;

    private int pageNum = 1;//请求页数，默认请求第一条
    public  boolean hasNext=true;
    private NoticeRepository noticeRepository=new NoticeRepository();

    boolean isLoading;
    private List<NoticeAgentMatterListOutput.DataBean.ListBean> data = new ArrayList<>();
    private NoticeListAdapter adapter = new NoticeListAdapter(this, data);

    String noticeType;
    String employeeId;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notice_list);
        ButterKnife.bind(this);

        initView();
        reqData();
    }

    public void initView() {
        thisActivity=this;
        noticeType=getIntent().getIntExtra("type",0)+"";
        employeeId = SPUtil.getString("empId");
        String title=this.setNoiceTitle(Integer.parseInt(noticeType));
        mToolbarCenterTv.setText(title);

        swipeRefreshLayout.setProgressViewOffset(false, 70, (int) TypedValue
                .applyDimension(TypedValue.COMPLEX_UNIT_DIP, 90, getResources()
                        .getDisplayMetrics()));
        swipeRefreshLayout.setColorSchemeResources(R.color.black);
        swipeRefreshLayout.post(new Runnable() {
            @Override
            public void run() {
                swipeRefreshLayout.setRefreshing(true);
            }
        });

        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                reqData();
            }
        });
        final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.addItemDecoration(new DividerItemDecoration(this,LinearLayoutManager.VERTICAL));
        recyclerView.setAdapter(adapter);
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                Log.d("test", "StateChanged = " + newState);


            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                Log.d("test", "onScrolled");

                int lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition();
                if (lastVisibleItemPosition + 1 == adapter.getItemCount()) {
                    if(thisActivity.hasNext){
                        Log.d("test", "loading executed");
                        boolean isRefreshing = swipeRefreshLayout.isRefreshing();
                        if (isRefreshing) {
                            adapter.notifyItemRemoved(adapter.getItemCount());
                            return;
                        }
                        if (!isLoading) {
                            isLoading = true;
                            reqData();
                        }
                    }else{

                    }
                }
            }
        });

        //添加点击事件
        adapter.setOnItemClickListener(new NoticeListAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Log.d("test", "item position = " + position);
            }

            @Override
            public void onItemLongClick(View view, int position) {

            }
        });
    }

    private String setNoiceTitle(int type){
        switch (type){
            case 1:
                return "营销浏览";
            case 2:
                return "人力浏览";
            case 3:
                return "培训浏览";
            case 4:
                return "金价浏览";
            case 5:
                return "运营浏览";
           /* case 6:
                return "生日提醒";*/
            case 14:
                return "七日通知";
            case 15:
                return "生日提醒";
        }
        return "通知";
    }

    public void reqNoticeAgentMatterList(){
        noticeRepository.reqNoticeAgentMatterList(pageNum,employeeId , noticeType ,new RequestCallBack() {
            @Override
            public void onSuccess(Object object) {
                NoticeAgentMatterListOutput noticeListOutput = (NoticeAgentMatterListOutput) object;
                Paginator paginator=noticeListOutput.getData().getPaginator();
                if(noticeListOutput.getData().getList().size()==0){

                }else if(thisActivity.hasNext){
                    data.addAll(noticeListOutput.getData().getList());
                    swipeRefreshLayout.setRefreshing(false);
                    adapter.notifyItemRemoved(adapter.getItemCount());
                    thisActivity.pageNum=thisActivity.pageNum+1;
                    thisActivity.hasNext=paginator.isHasNextPage();
                    adapter.notifyDataSetChanged();
                }else{
                    Log.d("test", "没有更多数据" );
                    adapter.notifyItemRemoved(adapter.getItemCount());
                }
                swipeRefreshLayout.setRefreshing(false);
            }

            @Override
            public void onFalse(String msg) {

            }
        });
    }

    public void reqAgentMatterList(){
        noticeRepository.reqAgentMatterList(pageNum,employeeId , noticeType ,new RequestCallBack() {
            @Override
            public void onSuccess(Object object) {
                NoticeAgentMatterListOutput noticeListOutput = (NoticeAgentMatterListOutput) object;
                Paginator paginator=noticeListOutput.getData().getPaginator();
                if(noticeListOutput.getData().getList().size()==0){

                }else if(thisActivity.hasNext){
                    data.addAll(noticeListOutput.getData().getList());
                    swipeRefreshLayout.setRefreshing(false);
                    adapter.notifyItemRemoved(adapter.getItemCount());
                    thisActivity.pageNum=thisActivity.pageNum+1;
                    thisActivity.hasNext=paginator.isHasNextPage();
                    adapter.notifyDataSetChanged();
                }else{
                    Log.d("test", "没有更多数据" );
                    adapter.notifyItemRemoved(adapter.getItemCount());
                }
                swipeRefreshLayout.setRefreshing(false);
            }

            @Override
            public void onFalse(String msg) {

            }
        });
    }

    public void reqData() {
        if("14".equals(noticeType)||"15".equals(noticeType)){
            reqAgentMatterList();
        }else{
            reqNoticeAgentMatterList();
        }
    }

    @OnClick(R.id.back_img)
    public void onClick() {
        startActivity(new Intent(NoticeListActivity.this,NoticeActivity.class));
        this.finish();
        overridePendingTransition(com.vincent.baseapp.R.anim.fragment_left_in, com.vincent.baseapp.R.anim.fragment_right_out);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_BACK){
            startActivity(new Intent(NoticeListActivity.this,NoticeActivity.class));
            this.finish();
            overridePendingTransition(com.vincent.baseapp.R.anim.fragment_left_in, com.vincent.baseapp.R.anim.fragment_right_out);
        }
        return super.onKeyDown(keyCode, event);
    }
}
